import { dingTalkM } from 'dingtalk-auth' //签名免登
import store from '@/store'
import { setToken } from '@/utils/token'

//当前配置 针对 "dingtalk-auth": "1.1.4" 版本
class DingM{

    constructor(){
        this.configApi = '/api/auth/getJsConfig?1=1'
        this.configList = [
            'biz.util.uploadImage', 
            'biz.contact.complexPicker', 
            'biz.clipboardData.setData'
        ]
    }

    async ddConfig(){
        const {corpId} = await dingTalkM.ddConfig(this.configList, this.configApi);
        this.corpId = corpId;
        return {code: 100}
    }

    async ddLogin(corpId){//免登接口
        const res = await dingTalkM.authLogin(corpId);
        return res;
    }

    async authLogin(){//签名 + 免登
        const {code} = await this.ddConfig();
        if(code === 100){
            const {token, user} = await this.ddLogin(this.corpId);
            await store.dispatch("setUser", user);
            await store.dispatch("setIsAdmin", user.isAdmin);
            let res = setToken(token);
            return true
        }
        return false
    }

}

const dingM = new DingM();
export default dingM;